import 'package:appflutterhomeschool/api/api_service.dart';
import 'package:appflutterhomeschool/api/dio_utils.dart';
import 'package:appflutterhomeschool/model/parent_get_homeworks_model.dart';
import 'package:appflutterhomeschool/model/tab_model.dart';
import 'package:appflutterhomeschool/view_model/my_app_view_model.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';

///用户： ninetailedfox
///时间： 2020/7/6
///邮箱： 1037438704@qq.com
///功能： 作业详情状态管理
class OperationDetailsViewModel extends ChangeNotifier {
  String id;

  List imageUrlList = [];
  String checkStatus = "1";

  BuildContext me;
  List<TabModel> listTab = [
    TabModel("待批", true),
    TabModel("已批", false),
    TabModel("未交", false),
  ];
  int getTabIndex = 0;
  bool identity;

  OperationDetailsViewModel(this.me, this.id);

//列表
  List<ParentGetHomeworksModel> parentGetHomeworkList;
  MyAppViewModel myAppViewModel;
  ParentGetHomeworksModel parentGetHomeworksModel;

  String title = "";

  initView() {
    myAppViewModel = Provider.of(me);

    identity = myAppViewModel.identity;
    if (identity) {
      listTab = [
        TabModel("进行中", true),
        TabModel("未批改", false),
        TabModel("已批改", false),
      ];
    }

    Dio2Utils.getInstance().get("${ApiService.teacherHomeworksPk}${id}/", {},
        (successCallBack) {
      List listData = successCallBack["data"];
      if (listData == null) {
        return;
      }
      if (listData.length <= 0) {
        return;
      }
      parentGetHomeworksModel =
          ParentGetHomeworksModel.fromJson(listData.first);
      print("==========${parentGetHomeworksModel.publishTime}");
      imageUrlList = parentGetHomeworksModel.imgs ?? [];
      notifyListeners();
    }, (errorCallBack) {});
    shaixuan();
  }

  shaixuan() {
//    homework/teacher_homeworks/cls_mapping_id/2/status/1/
    if (identity) {
//      1: 进行中; 2: 未批改; 3 :已批改
      Dio2Utils.getInstance().get(
          "${ApiService.teacherHomeworksClass}${myAppViewModel.studentInformationModel.classMappingId}/status/$checkStatus/",
          {}, (successCallBack) {
        List listData = successCallBack["data"];
        parentGetHomeworkList = [];
        if (listData == null) {
          return;
        }
        listData.forEach((element) {
          parentGetHomeworkList.add(ParentGetHomeworksModel.fromJson(element));
        });
//        title = parentGetHomeworkList[0].getTitleStatus(myAppViewModel.identity);
        notifyListeners();
      }, (errorCallBack) {});
    } else {
      //    # 1: 未批改; 2: 已批改; 3: 未提交;）
      Dio2Utils.getInstance().get(
          "${ApiService.teacherHomeworksPkTWid}${id}/check_status/$checkStatus/",
          {}, (successCallBack) {
        List listData = successCallBack["data"];
        parentGetHomeworkList = [];
        if (listData == null) {
          return;
        }
        listData.forEach((element) {
          parentGetHomeworkList.add(ParentGetHomeworksModel.fromJson(element));
        });
        notifyListeners();
      }, (errorCallBack) {});
    }
  }

  tapModelOnTap(int index) {
    getTabIndex = index;
    for (int i = 0; i < listTab.length; i++) {
      listTab[i].display = false;
    }
    listTab[index].display = true;

    checkStatus = "${index + 1}";
    shaixuan();
    notifyListeners();
  }

  //提醒
  remindOnTap(int index) {
    Dio2Utils.getInstance().post(ApiService.remindHomework, {
      "student_homework_id": parentGetHomeworkList[index].id
    }, (successCallBack) {
      Fluttertoast.showToast(msg: successCallBack["message"]);
    }, (errorCallBack) {});
  }

}